<?php
if (!defined('BASEPATH'))
	exit('No direct script access allowed');

class Admin extends CI_Controller {
	private $tb_article_category = 'article_category';
	private $tb_product_category = 'product_category';
	private $tb_lienhe = 'contact';
	private $tb_article = 'article';
	private $tb_product = 'product';
	private $tb_quangcao = 'quangcao';
	private $tb_setting = 'setting';
	//phan trang
	private $base_url_trang = '#';
	private $total_rows = 100;
	private $per_page = 10; // bang voi limit
	private $limit = 10; //bang voi perpage
	///
	private $sochu = 20; //chon so chua cat dua vao intro bai viet
	private $id = '';
	public $sele = '';
	private $categoryid = 0;
	/**
	 * 
	 * thay doi field trong file view cho dung voi bang hien = publish 
	 */
	function __construct() {
		parent::__construct();
		$this->load->library('session');
		$this->load->helper(array(
			'form',
			'text',
			'chuyendoitv_helper',
			'url'
		));
		//$this->lang->load('tank_auth'); 
		$this->load->model('Admin_model');
		$this->load->library(array(
			'pagination',
			'form_validation',
			'security'
		));
		if ($this->auth()) {
		} else
			redirect('dangnhap');
	}
	function auth() {
		if ($this->session->userdata('logedin')) {
			if ($this->session->userdata('id') != '') {
				$this->session->set_userdata('upanh', $this->session->userdata('id')); //nho bo khi hoan thanhv 
				return true;
			}
		} else {
			$this->session->set_userdata('auth', 'Bạn không có quyền truy cập');
			return false;
		}
	}
	public function index() {
		redirect('admin/bang-chinh');
	}
	function ajax() {
		if (!empty($_POST)) {
			$data = $this->security->xss_clean($_POST['data']);
		}
		$table = $this->security->xss_clean($this->uri->segment(3, 0)); //bang
		$field = $this->security->xss_clean($this->uri->segment(4, 0)); //truong cua bang
		$uri5  = $this->security->xss_clean($this->uri->segment(5, 0)); //id cua bai viet can thay doi
		$uri6  = $this->security->xss_clean($this->uri->segment(6, 0)); //id cua div can change
		$data  = array(
			$field => $data
		);
		$this->db->where('id', $uri5);
		$this->db->update($table, $data);
		echo '#' . $uri6; // echo gui id ve 
	}
	function ajax_field() {
		if (!empty($_POST)) {
			$data = $this->security->xss_clean($_POST['data']);
		}
		$uri3 = $this->uri->segment(3, 0);
		$uri4 = $this->uri->segment(4, 0); //bang        
		if (!empty($data)) {
			switch ($uri3) {
				case 'order':
					$this->session->set_userdata('order', $data);
					if ($this->session->userdata('sapxep')) {
						if ($this->session->userdata('sapxep') == 'ASC') {
							$this->session->set_userdata('sapxep', 'DESC');
						} else
							$this->session->set_userdata('sapxep', 'ASC');
					} else
						$this->session->set_userdata('sapxep', 'ASC');
					break;
				case 'num':
					$this->session->set_userdata('limit', $data);
					break;
				case 'pub':
					$data   = $this->security->xss_clean($_POST);
					$id     = $data['id'];
					$pulish = $data['publish'];
					$tba    = $this->uri->segment(4, 0);
					$this->db->where('id', $id);
					$this->db->update($tba, array(
						'publish' => $pulish
					));
					break;
				case 'where':
					if ($uri4 == 'article') {
						switch ($data) {
							case 'all':
								$this->session->set_userdata('all_article', 1);
								$this->session->unset_userdata('value_where');
								$this->session->unset_userdata('where');
								break;
							default:
								$this->session->set_userdata('where', 'category');
								$this->session->set_userdata('value_where', $data);
								$this->session->set_userdata('all_article', 0);
								break;
						}
					}
					if ($uri4 == 'question_test') {
						switch ($data) {
							case 'all':
								$this->session->set_userdata('all_article', 1);
								$this->session->unset_userdata('value_where');
								$this->session->unset_userdata('where');
								break;
							default:
								$this->session->set_userdata('where', 'id_subjects');
								$this->session->set_userdata('value_where', $data);
								$this->session->set_userdata('all_article', 0);
								break;
						}
					}
					if ($uri4 == 'product') {
						switch ($data) {
							case 'all':
								$this->session->set_userdata('all_product', 1);
								$this->session->unset_userdata('value_where');
								$this->session->unset_userdata('where');
								break;
							default:
								$this->session->set_userdata('where', 'category');
								$this->session->set_userdata('value_where', $data);
								$this->session->set_userdata('all_product', 0);
								break;
						}
					}
					if ($uri4 == 'faqs') {
						switch ($data) {
							case 'all':
								$this->session->set_userdata('all_product', 1);
								$this->session->unset_userdata('value_where');
								$this->session->unset_userdata('where');
								break;
							default:
								$this->session->set_userdata('where', 'type');
								$this->session->set_userdata('value_where', $data);
								$this->session->set_userdata('all_product', 0);
								break;
						}
					}
					break;
			}
		}
	}
	function ajax_search() {
		if ($_POST) {
			$this->session->set_userdata('keyword', $this->security->xss_clean($_POST['keyword']));
			
		}
		$uri4 = $this->uri->segment(4, 0); //bang
		if ($uri4 == 'product') {
			$query      = $this->db->get('product');
			$data_seach = $query->result();
			if (!empty($atta)) {
				if (!empty($keyword)) {
					$this->db->like('title', $keyword);
				}
				$query = $this->db->get('product');
			} else {
				echo "Không tìm thấy dữ kiện nào phù hơp với từ khóa của bạn!";
			}
		}
	}
	function xuly() {
		//$this->session->set_userdata('order',0);
		$table        = $this->security->xss_clean($this->uri->segment(3, 0)); //su chu run
		$dk           = $this->security->xss_clean($this->uri->segment(4, 0)); //sau chu run
		$id           = $this->security->xss_clean($this->uri->segment(5, 0)); //lay id
		$this->id     = $id;
		$alias 		  = '';
		if($table == 'thanhtich'){
			$this->rounte = 'users';
			$alias = 'thanhtich';
		} else {
			$this->rounte = $table;
		}
		if (!empty($_POST)) {
			if ($this->validate()) {
				if ($table == 'gv_hs') {
					$mailhs = $_POST['email_p'];
					$mailgv = $_POST['email_t'];
					$num    = $this->common_model->getnumber2('users', array(
						'email' => $mailhs,
						'type' => 0
					));
					$num2   = $this->common_model->getnumber2('users', array(
						'email' => $mailgv,
						'type' => 1
					));
					if (!$num) {
						$this->session->set_userdata('thongbao1', 'Email học sinh không tồn tại');
						$dk = 'edit';
					} elseif (!$num2) {
						$this->session->set_userdata('thongbao2', 'Email giáo viên không tồn tại');
						$dk = 'edit';
					} else {
						$post_data = $_POST;
					}
				} else {
					$post_data = $_POST;
				}
			} else {
				$dk = 'edit';
			}
		}
		if (empty($post_data)) {
			$post_data = '';
		}
		$data['page']        = $this->run($dk, $id, $post_data, $alias);
		$data['setting']     = $this->Admin_model->get_one($this->tb_setting);
		$data['contact_new'] = $this->Admin_model->get_number($this->tb_lienhe, array(
			'type' => 0
		));
		if (empty($data['contact_new'])) {
			$data['contact_old'] = $this->Admin_model->get_number($this->tb_lienhe, array(
				'type' => 1
			));
		}
		$this->load->view('admin/index', $data);
	}
	
	function dashboard() {
		$data2['giaovien'] = $this->common_model->getdata('users', array(
			'type' => 1
		), array(
			'created',
			'email',
			'name',
			'type',
			'id'
		), array(
			'id',
			'DESC'
		), 5);
		$data2['hocsinh']  = $this->common_model->getdata('users', array(
			'type' => 0
		), array(
			'created',
			'email',
			'name',
			'type',
			'id'
		), array(
			'id',
			'DESC'
		), 5);
		$data2['yeucau']   = $this->common_model->getdata('request', array(
			'publish' => 0
		), '', array(
			'id',
			'DESC'
		), 5);
		$data2['setting']  = $this->common_model->getdata('setting', array(
			'id' => 1
		));
		$data['page']      = $this->load->view('admin/content/dashboard_list.php', $data2, true);
		$this->load->view('admin/index', $data);
	}
	function run($dk, $id, $post_data = '', $alias = '') {
		
		if (!is_numeric($id)) {
			$id = 0;
		}
		if (!empty($this->rounte)) {
			$table = $this->rounte;
			if($alias == 'thanhtich'){
				$page = $alias;
			} else {
				$page  = $this->rounte;
			}
		} else {
			$table = ''; //chua kiem tra co bang hay chua
		}
		if (!empty($dk)) {
			switch ($dk) {
				case 'list':
					if (!empty($post_data)) {
						$data = $post_data;
						$data = $this->xuly_dulieu($data, $table, $id);
						if ($id) {
							if ($table == 'email') {
								$this->db->where('id', $id);
								$this->db->update($table, $data);
								if ($data['publish'] == 1) {
									$url  = 'http://10cong.com/email/soan/' . $id . '.html';
									$user = $this->common_model->getone('users', array(
										'email' => $data["email_from"]
									));
									$img  = explode(',', $user->avartar);
									$avar = get_thumnails($img, 140, 170);
									if ($user->type == 1) {
										$type         = 'Giáo viên';
										$mailgiaovien = $user->email;
										$ttgs         = '<td style="vertical-align:top">
                                                                          <table style="width:144px">
                                                                             <tbody>
                                                                                <tr>
                                                                                   <td><img alt="[Ảnh đại diện của ' . $user->name . ']" src="' . $avar . '" width="140px" height="170"></td>
                                                                                </tr>
                                                                                <tr>
                                                                                   <td style="font-size:13px">
                                                                                      <a href="' . base_url() . 'thong-tin-gia-su.' . $user->id . '.html" target="_blank">' . $user->name . '</a><br>
                                                                                   </td>
                                                                                </tr>
                                                                                <tr>
                                                                                   <td style="font-size:12px">
                                                                                      <b>Học Phí</b>&nbsp;' . number_format($user->mucluong) . '
                                                                                   </td>
                                                                                </tr>  
                                                                             </tbody>
                                                                          </table>
                                                                       </td>';
									} else {
										$type         = 'Học Sinh';
										$mailgiaovien = $data->email_from;
										$ttgs         = '';
									}
									$url = 'http://10cong.com/email/chi-tiet/' . $id . '.html';
									date_default_timezone_set('GMT');
									$this->load->library('email');
									$this->email->set_newline("\r\n");
									$config['mailtype']  = 'html';
									$config['proxy_ips'] = '';
									$config['protocol']  = 'smtp';
									$config['smtp_host'] = 'ssl://smtp.googlemail.com';
									$config['smtp_user'] = 'info.10cong@gmail.com';
									$config['smtp_pass'] = '10cong.com';
									$config['smtp_port'] = '465';
									$this->email->initialize($config);
									$this->email->from('info.10cong@gmail.com', '10+ cổng trao đổi thông tin trực tuyến giữa giáo viên và học sinh');
									$this->email->to($data['email_to']);
									$this->email->subject($data['subjects']);
									$this->email->message('
                                                           <table height="84" width="100%" border="0" cellpadding="0" cellspacing="0">
                                                              <tbody>
                                                                 <tr>
                                                                    <td valign="top">
                                                                       <img src="' . base_url() . 'skin/front-end/images/logo.png" alt="10 cộng" style="margin:0;border:0px none;padding:0;display:inline" height="84" width="210">
                                                                    </td>
                                                                    <td valign="bottom" align="right">
                                                                       <a href="' . base_url() . 'dang-ky-gia-su.html" style="font-size:11px;font-weight:bold;color:#3c709a;margin:15px 3px;padding:0px" target="_blank">Đăng kí gia sư</a>
                                                                       <a href="' . base_url() . 'tim-gia-su.html" style="font-size:11px;font-weight:bold;color:#3c709a;margin:15px 3px;padding:0px" target="_blank">Tìm gia sư</a>
                                                                       <a href="' . base_url() . 'gioi-thieu.html" style="font-size:11px;font-weight:bold;color:#3c709a;margin:15px 3px;padding:0px" target="_blank">Về 10+</a>
                                                                       <a href="' . base_url() . 'tro-giup.html" style="font-size:11px;font-weight:bold;color:#3c709a;margin:15px 3px;padding:0px" target="_blank">Trợ giúp</a>
                                                                    </td>
                                                                 </tr>
                                                              </tbody>
                                                           </table>
                                                           <div style="padding:10px;border:2px solid #e0e0e0;margin:10px 0 0 0;font-size:12px;line-height:1.5em;color:#4f4f4f;background-color:#fafafa">
                                                              <table style="line-height:1.4em">
                                                                 <tbody>
                                                                    <tr>
                                                                       <td style="vertical-align:top">
                                                                          <table>
                                                                             <tbody>
                                                                                <tr>
                                                                                   <td style="font-size:13px">
                                                                                      <div style="margin-top:10px">
                                                                                        <h2>' . $type . ' vừa trả lời bạn tại cộng đồng 10+ : </h2>
                                                                                         ' . $data["description"] . '
                                                                                      </div>
                                                                                      <div>
                                                                                     <a href="' . $url . '" title="trả lời" style="text-align:center;color:#fff;display:block;margin:10px auto;width:250px;padding:25px 15px;font-size:18px;background:#ff9900;text-decoration:none;">Trả lời</a>
                                                                                      <p>Vui lòng không sử dụng hệ thống của Gmail để trả lời email này.</p>
                                                                                      </div>
                                                                                   </td>
                                                                                </tr>
                                                                             </tbody>
                                                                          </table>
                                                                       </td>
                                                                       ' . $ttgs . '
                                                                    </tr>
                                                                 </tbody>
                                                              </table>
                                                        </div>
                                    ');
									$this->email->send();
								}
								$this->session->set_userdata('thongbao', 'Đã Sửa xong');
							} else {
								$this->db->where('id', $id);
								$this->db->update($table, $data);
								$this->session->set_userdata('thongbao', 'Đã Sửa xong');
							}
						} else {
							$this->db->insert($table, $data);
							$this->session->set_userdata('thongbao', 'Đã thêm');
						}
						redirect(base_url() . 'admin/xuly/' . $page . '/list');
					}
					//*****************************************hien thi danh sach**********************************
					$this->base_url_trang = base_url() . 'admin/xuly/' . $page . '/list/';
					if ($table != $this->session->userdata('sec_table')) {
						//check xem co trung bang hay khong de kiem tra xem dang o trang cu hoac trang moi xoa session//bang ba
						// bang bang session bang thi kiem tra order_by
						$this->session->set_userdata('order', 0);
						$this->session->set_userdata('sec_table', $table);
						$this->session->set_userdata('where', 0);
					}
					if ($this->session->userdata('order')) {
						$order = array(
							'truong' => $this->session->userdata('order'),
							'sapxep' => $this->session->userdata('sapxep')
						);
					} else
						$order = array();
					
					// limit start dung sau 
					if (!$this->session->userdata('limit')) {
						$this->session->set_userdata('limit', $this->limit);
					} else
						$this->limit = $this->session->userdata('limit');
					$this->per_page = $this->limit; // so record tren 1 trang bang limit
					if ($id) {
						$start_limit = $id;
					} else
						$start_limit = 0;
					if ($this->session->userdata('change_cat')) {
						$start_limit = 0;
						$this->session->set_userdata('change_cat', 0);
					}
					$limit = $this->limit;
					
					//where
					if ($this->session->userdata('where')) {
						$where = $this->session->userdata('where');
						$value = $this->session->userdata('value_where');
						$where = array(
							$where => $value
						);
					} elseif ($this->uri->segment(5) == 'giaovien') {
						$where = array(
							'type' => 1
						);
					} elseif ($this->uri->segment(5) == 'hocsinh') {
						$where = array(
							'type' => 0
						);
					} elseif ($table == 'des_sub') {
						$where = array(
							'description !=' => ''
						);
					} else {
						$where = array();
					}
					//
					
					$this->total_rows = $this->Admin_model->get_number($table, $where);
					$this->phantrang();
					//thong baohien so ket qua tren tong so record
					$tt_limit = $this->limit + $id;
					if ($this->limit >= $this->total_rows) {
						$total = $this->total_rows;
					} else
						$total = $tt_limit;
					$result_mensenger = "danh sách từ $id - $total  của $this->total_rows";
					
					$cat = $this->cat($table);
					if ($table == 'email') {
						$this->db->order_by('publish', 'ASC');
						$this->db->order_by('id', 'DESC');
					}
					if ($table == 'reviews') {
						$this->db->order_by('publish', 'ASC');
						$this->db->order_by('id', 'DESC');
					}
					if ($page == 'thanhtich') {
						$this->db->order_by('thanhtich', 'DESC');
						$this->db->order_by('bangcap', 'DESC');
						$this->db->order_by('bangdiem', 'DESC');
						$this->db->order_by('chungchi', 'DESC');
						$this->db->order_by('thanhtich1', 'ASC');
						$this->db->order_by('thanhtich2', 'ASC');
						$this->db->order_by('thanhtich3', 'ASC');
						$this->db->order_by('thanhtich4', 'ASC');
						$this->db->order_by('thanhtich5', 'ASC');
					}
					$data = array(
						'data' => $this->Admin_model->get_data($table, $where, $order, $limit, $start_limit),
						'result_mensenger' => $result_mensenger,
						'cat' => $cat
					);
					return $this->load->view('admin/content/' . $page . '_list', $data, true); //true la k hien chi tra ve
					break;
				case 'edit':
					//hien thi sua                    
					if ($id) {
						if ($table == 'article') {
							$this->categoryid = $this->Admin_model->get_one($table, array(
								'id' => $id
							))->category;
						}
						$cat  = $this->cat($table);
						$data = array(
							'data' => $this->Admin_model->get_one($table, array(
								'id' => $id
							)),
							'cat' => $cat
						);
						
					} else {
						$cat  = $this->cat($table);
						$data = array(
							'cat' => $cat
						);
					}
					return $this->load->view('admin/content/' . $page . '_edit', $data, true); //true la k hien chi tra ve
					break;
				case 'xoa':
					if ($id) {
						$this->db->delete($table, array(
							'id' => $id
						));
						$this->session->set_userdata('thongbao', 'Đã thực hiện');
						$ur6 = $this->uri->segment(6);
						if ($ur6) {
							redirect(base_url() . 'admin/xuly/' . $page . '/list/' . $ur6);
						} else
							redirect(base_url() . 'admin/xuly/' . $page . '/list');
					}
					break;
			}
		}
	}
	function xuly_dulieu($data, $table, $id = '') {
		if ($table == 'users') {
			if (!empty($data['new_password_key']) && trim($data['new_password_key']) != '') {
				$data['password'] = sha1(md5($data['new_password_key']));
			}
		}
		if (!empty($data['metades'])) {
			$data['metades'] = trim($data['metades']);
		}
		if (!empty($data['metakey'])) {
			$data['metakey'] = trim($data['metakey']);
		}
		//xu ly anh product           
		if ($table == 'product' && !empty($data['img'])) {
			$image       = $data['img'];
			$data['img'] = implode(',', $image);
		}
		//en xu ly anh product
		// san pham
		if (($table == 'article_category') || ($table == 'article') || ($table == 'product') || ($table == 'product_category') || ($table == 'subjects')) {
			if (empty($data['alias'])) {
				$data['alias'] = creat_alias($data['title']);
			} else
				$data['alias'] = creat_alias($data['alias']);
			$alias    = $data['alias'];
			$alias2   = $data['alias'];
			$dieukien = 3;
			while ($dieukien) {
				if (!empty($id)) {
					$nd = array(
						'alias' => $alias,
						'id !=' => $id
					);
				} else
					$nd = array(
						'alias' => $alias
					);
				$check = $this->Admin_model->check_trung($nd, $table);
				if ($check) {
					$dieukien = $dieukien + 1;
					$alias    = $alias2 . $dieukien;
				} else
					$dieukien = 0;
			}
			$data['alias'] = $alias;
			//xu ly bai viet            
			if (($table == 'article') || ($table == 'product')) {
				if (empty($data['image'])) {
					$ddd           = $data['fulltext'] . '<img src=""/>'; //'images/default.jpg'
					$diachianh     = laydiachianh($ddd); //lay dia chi anh
					$data['image'] = $diachianh;
				}
				if (empty($data['introtext'])) {
					//character_limiter($row_baiviet->title,35);
					//xu ly introtext
					$loaibohtml        = loaibo_html($data['fulltext']); //loai bo ma html       
					$loaibohtml        = word_limiter($loaibohtml, $this->sochu); // lay 30 chu
					$loaibohtml        = trim($loaibohtml);
					$data['introtext'] = $loaibohtml;
					$data['fulltext']  = loaibo_div_html($data['fulltext']);
					if (empty($data['metades'])) {
						$data['metades'] = $loaibohtml;
					}
					
				}
				
			} //en xu ly anh+ meta des bai viet
			
			if (empty($data['metakey']) || empty($data['metades'])) {
				$setting = $this->Admin_model->get_one('setting', array(
					'id' => 1
				));
				if (empty($data['metakey'])) {
					$data['metakey'] = $setting->metakey;
				}
				if (empty($data['metades'])) {
					$data['metades'] = $setting->metades;
				}
			}
		}
		//en danh muc voi tin tuc       
		return $data;
	}
	function cat($table) {
		if ($this->id) {
			$where = array(
				'id !=' => $this->id
			);
		} else
			$where = '';
		switch ($table) {
			case 'article_category':
				return $this->getid($this->Admin_model->get_data($table, $where));
				break;
			case 'article':
				$category = $this->getid($this->Admin_model->get_data('article_category'));
				return array(
					'category' => $category
				);
				break;
			case 'subjects':
				return $this->getid($this->Admin_model->get_data($table, $where));
				break;
			case 'faqs':
				return $this->Admin_model->get_data('faqs', array(
					'type' => 0
				));
				break;
			case 'map':
				return $this->Admin_model->get_data('map', array(
					'publish' => 1
				));
				break;
			case 'polls_category':
				return $this->Admin_model->get_data($table, $where);
				break;
			case 'polls':
				return $this->Admin_model->get_data($this->tb_polls_category);
				break;
			default:
				return true;
				break;
		}
		
	}
	function validate() {
		
		switch ($this->rounte) {
			case 'hotro':
				$this->form_validation->set_rules('name', 'Tên', 'required|xss_clean|min_length[4]|max_length[20]');
				$this->form_validation->set_rules('yahoo', 'Yahoo', 'required|xss_clean|min_length[4]|max_length[20]');
				$this->form_validation->set_rules('mobile', 'Điện thoại', 'required|xss_clean|numeric|min_length[9]|max_length[12]');
				break;
			case 'article':
				$this->form_validation->set_rules('title', 'Tên danh mục', 'required|xss_clean|min_length[2]');
				$this->form_validation->set_rules('noibat', 'nổi bật', 'numeric');
				break;
			case 'article_category':
				$this->form_validation->set_rules('title', 'Tên danh mục', 'required|xss_clean|min_length[2]');
				$this->form_validation->set_rules('position', 'Vị trí', 'required|numeric');
				$this->form_validation->set_rules('publish', 'Hiển thị', 'required|numeric');
				break;
			default:
				return true;
				
		}
		if ($this->form_validation->run() == true) {
			return true;
		} else
			return false;
	}
	function phantrang() {
		$this->load->library('pagination');
		$config['base_url']        = $this->base_url_trang;
		$config['total_rows']      = $this->total_rows;
		$config['per_page']        = $this->per_page;
		$config['uri_segment']     = 5;
		$config['num_links']       = 10;
		$config['first_link']      = 'Đầu';
		$config['first_tag_open']  = '<li class="firt_pag">'; //div cho chu <<
		$config['first_tag_close'] = '</li>'; //div cho chu <<
		$config['last_link']       = 'Cuối';
		$config['last_tag_open']   = '<li class="last_pag">';
		$config['last_tag_close']  = '</li>';
		//-------next-
		$config['next_link']       = 'Trang Sau';
		$config['next_tag_open']   = '<li>';
		$config['next_tag_close']  = '</li>';
		//------------preview
		$config['prev_link']       = 'Trang Trước';
		$config['prev_tag_open']   = '<li>';
		$config['prev_tag_close']  = '</li>';
		// ------------------cu?npage
		$config['cur_tag_open']    = '<li class="active"><a tabindex="0">';
		$config['cur_tag_close']   = '</a></li>';
		//--so 
		$config['num_tag_open']    = '<li>';
		$config['num_tag_close']   = '</li>';
		//-----
		$this->pagination->initialize($config);
		
	}
	function logout() {
		$this->session->unset_userdata('id');
		$this->session->unset_userdata('logedin');
		redirect('dangnhap');
	}
	
	///// get category cho muc chon danh muc cha
	function add_rep($num) {
		$prep = '';
		for ($i = 0; $i <= $num - 1; $i++) {
			$prep .= ' +--';
		}
		return $prep;
	}
	function getid($dat) {
		if ($dat) {
			foreach ($dat as $v) {
				$data[$v->parentid][] = $v;
			}
			
			$this->menucreat($data, 0);
		}
		return $this->sele;
	}
	function menucreat($data, $id = 0, $leve = 0) {
		$leve++;
		foreach ($data[$id] as $dulieu) {
			$this->sele .= '<option id= "' . $dulieu->id . '" value = "' . $dulieu->id . '">';
			
			if ($id == 0) {
				$this->sele .= $this->add_rep($leve) . $dulieu->title;
			} else {
				$this->sele .= $this->add_rep($leve) . $dulieu->title;
			}
			
			
			if (!empty($data[$dulieu->id])) {
				$this->menucreat($data, $dulieu->id, $leve);
			}
			$this->sele .= '</option>';
		}
		$leve--;
		
	}
}
?>